// app.js
const utils = require("./utils/util");
App({
  onLaunch() {
    let self = this;
    wx.setNavigationBarColor({
      backgroundColor: '#8fd3f4',
      frontColor: '#ffffff'
    })
  },
  async wxLogin() {
    return new Promise(resolve => {
      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          if (res.code) {
            wx.request({
              url: 'https://api.liandunkaoqin.com/api/toolapi/opid',
              header: {
                'content-type': 'application/json;charset=UTF-8;' // 默认值
              },
              method: "POST",
              data: {
                appid: "wxb181da5dfd5a9516",
                secret: "603e44053d4ba0b7999b02cf9018fe53",
                js_code: res.code,
                grant_type: "authorization_code"
              },
              success: res => {
                resolve(res.data.unionid);
              }
            })
          }
        }
      })
    });
  },
  async login() {
    return new Promise(resolve => {
      this.wxLogin().then(res => {
        let unionid = res;
        wx.request({
          url: 'https://api.liandunkaoqin.com/api/adminapi/signin',
          method: "POST",
          data: {
            "signin_type": "wx",
            "key": utils.encrypt(unionid)
          },
          success: res => {
            if (res.data.ret == 0) {
              this.globalData.mode=res.data.user_mode;
              this.globalData.unionid = utils.decrypt(res.data.openid);
              resolve(res.data.token);
            } else {
              wx.redirectTo({
                url: `/pages/404/404?error=0&msg=${res.data.msg}`,
              })
            }
          }
        })
      })
    });
  },
  globalData: {
    cost_type: "",
    unionid: "",
    token: "",
    mode:""
  }
})